﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SF.Base.Core;
using System.Collections;
using System.Text;

namespace SF.Dict.Web {
	public partial class Home : BasePage {

		protected void Page_Load(object sender, EventArgs e) {

			if (!Page.IsPostBack) {

				if (Global.DTUser == null) {
					Response.Redirect("/login.aspx?return=home.aspx");
				}

				UserEntity user = Global.DTUser;

				lbl_name.Text = user.NiceName;
				string AbsoluteUri = Request.Url.AbsoluteUri;
				lbl_domain.Text = AbsoluteUri.Substring(0, AbsoluteUri.LastIndexOf("/")).Replace("http://", "") + "/user.aspx?u=" + user.UID;

				hl_avatar.Text = user.NiceName;
				hl_avatar.NavigateUrl = "/user.aspx?u=" + user.UID;
				
				if (!string.IsNullOrEmpty(user.UserMeta["PROFILEIMAGEURL"])) {
					hl_avatar.Attributes.Add("style", "background-image: url(" + user.UserMeta["PROFILEIMAGEURL"] + ")");
				}
				else{
					hl_avatar.Attributes.Add("style", "background-image: url(/Common/images/avatar_64_64.png)");
				}

				//获取用户关注的词汇表
				List<VocabularyEntity> UserVocs = VocabularyRules.GetUserFollowVocabularyByUserId(UserId);
				lbl_voccount.Text = UserVocs.Count.ToString();
				rp_uservocs.DataSource = UserVocs;
				rp_uservocs.DataBind();

				VocabularyEntity voc = VocabularyRules.GetVocabulary(2);

				if (voc == null) {
					//错误处理
				}

				//查询词汇表下全部心得
				List<PostEntity> PostLists = PostRules.GetPostsByUserId(UserId);

				ArrayList AlPost = new ArrayList();

				foreach (PostEntity p in PostLists) {
					if (!AlPost.Contains(p.PostDateDay)) {
						AlPost.Add(p.PostDateDay);
					}
				}

				StringBuilder sb = new StringBuilder();

				sb.Append("<ul>");

				foreach (string day in AlPost) {
					List<PostEntity> PostListsForDay = PostLists.FindAll(delegate(PostEntity p){return p.PostDateDay == day;});
					string date = day;
					if (day == DateTime.Now.ToString("yyyy-MM-dd")) {
						date = "今天";
					}

					sb.AppendFormat("<li><h2>{0}</h2></li>", date);

					foreach (PostEntity p in PostListsForDay) {
						sb.AppendFormat("<li><div class='post' id='p_{0}'>{1}</div><div class='post-ft' id='p_{0}_r'></div></li>", p.Pid.ToString(), p.Content);
					}
				}

				sb.Append("</ul>");
				ltl_posts.Text = sb.ToString();
			}
		}


	}
}